<template>
    <div class="regist">
        <!--<pre v-text="$attrs"/>-->
      <el-card class="box-card" shadow="never">
        <div slot="header" class="clearfix">
          <span>注册</span>
        </div>
        <div style="margin:auto; width:400px;">
          <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
              <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
              <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="年龄" prop="age">
              <el-input v-model.number="ruleForm.age"></el-input>
            </el-form-item>
            <el-form-item label="性别" prop="gender">
              <el-select v-model="ruleForm.gender" placeholder="请选择性别">
                <el-option label="男" value="男"></el-option>
                <el-option label="女" value="女"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
</template>

<script>
  import axios from "../../plugins/axios";


    export default {
      data() {
        var validateName = (rule,value,callback)=>{
          if (!value) {
            return callback(new Error('用户名不能为空'));
          }
          setTimeout(() => {
            if(value.length<3||value.length>16){
              callback(new Error('用户名长度在3到16位之间'));
            }else{
              axios.post('/user/checkUsername',{'username':value})
                .then(function(response){
                  console.log(response)
                  if(response.data.data){
                    callback()
                  }else {
                    callback(new Error("用户名已被占用"))
                  }
                })
                .catch(function(response){
                  console.log(response)
                })
            }
          }, 1000);
        };
        var checkAge = (rule, value, callback) => {
          if (!value) {
            return callback(new Error('年龄不能为空'));
          }
          setTimeout(() => {
            if (!Number.isInteger(value)) {
              callback(new Error('请输入数字值'));
            } else {
              if (value < 18) {
                callback(new Error('必须年满18岁'));
              } else {
                callback();
              }
            }
          }, 1000);
        };
        var validatePass = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请输入密码'));
          } else {
            if (this.ruleForm.checkPass !== '') {
              this.$refs.ruleForm.validateField('checkPass');
            }
            callback();
          }
        };
        var validatePass2 = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请再次输入密码'));
          } else if (value !== this.ruleForm.pass) {
            callback(new Error('两次输入密码不一致!'));
          } else {
            callback();
          }
        };
        return {
          ruleForm: {
            pass: '',
            checkPass: '',
            age: '',
            username:'',
            gender:''
          },
          rules: {
            username:[
              { validator: validateName, trigger: 'blur' }
            ],
            pass: [
              { validator: validatePass, trigger: 'blur' }
            ],
            checkPass: [
              { validator: validatePass2, trigger: 'blur' }
            ],
            age: [
              { validator: checkAge, trigger: 'blur' }
            ]
          }
        };
      },
      methods: {
        submitForm(formName) {
          let that = this;
          this.$refs[formName].validate((valid) => {
            if (valid) {
              let param={
                "username":that.ruleForm.username,
                "password":that.ruleForm.pass,
                "age":that.ruleForm.age,
                "sex":that.ruleForm.gender
              };
              console.log(param);
              axios.post("/user/register",param)
                .then(function (response) {
                  if(response.data.code==1000){
                    that.$message({
                      message: '注册成功',
                      type: 'success',
                      center: true,
                      onClose: function () {
                        console.log("该跳转了")
                        that.$router.push({path:'/login/login'})
                      }
                    });
                  }
                }).catch(function(response){
                  console.log(response)
                })
            } else {
              console.log('error submit!!');
              return false;
            }
          });
        },
        resetForm(formName) {
          this.$refs[formName].resetFields();
        }
      }
    }
    //     props: {
    //     },
    // };
</script>
<style>
  .box-card {
    margin:20px auto;
    width: 600px;
  }
</style>
